﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraCharts;
using DevExpress.XtraReports.UI;

namespace ReceivingDelivering.ReportGenerators
{
    public partial class ReportGraphicInstock : DevExpress.XtraReports.UI.XtraReport
    {
        public ReportGraphicInstock()
        {
            InitializeComponent();
        }
        public void GeneraReport(DateTime dtAtTime)
        {
            lblAtTime.Text = dtAtTime.ToString("dd/MM/yyyy");
            DataTable dt = new DataTable();
            dt.Columns.Add("Argument", typeof(Int32));
            dt.Columns.Add("Name", typeof (string));
            dt.Columns.Add("Value", typeof(decimal)).DefaultValue = 0;

            string query = "SELECT [t5].[ProductNo], [t5].[ProductName], [t5].[Unit], \n" +
                        "[t5].[DeliveringPrice], [t5].[ReceivingPrice], \n" +
                        "[t5].[value] AS [InStockQuantity] \n" +
                        "FROM ( \n" +
                        "    SELECT [t0].[ProductNo], [t0].[ProductName], [t0].[Unit], [t0].[DeliveringPrice], [t0].[ReceivingPrice], (COALESCE(( \n" +
                        "        SELECT SUM([t1].[ReceivingQuantity]) \n" +
                        "        FROM [dbo].[ReceivingBillInfo] AS [t1] \n" +
                        "        INNER JOIN [dbo].[ReceivingBill] AS [t2] ON [t2].[ReceivingID] = [t1].[ReceivingID] \n" +
                        "        WHERE ([t2].[ReceivingCreateDate] <= @ThoiDiem)  \n" +
                        "        AND ([t2].[ReceivingCreateDate] >= [t0].[ReceivedDate])  \n" +
                        "        AND ([t1].[ProductID] = [t0].[ProductID]) \n" +
                        "        ),0)) - (COALESCE(( \n" +
                        "        SELECT SUM([t3].[DeleveringQuantity]) \n" +
                        "        FROM [dbo].[DeliveringBillInfo] AS [t3] \n" +
                        "        INNER JOIN [dbo].[DeliveringBill] AS [t4] ON [t4].[DeleveringID] = [t3].[DeleveringBillID] \n" +
                        "        WHERE ([t4].[CreateDate] <= @ThoiDiem) \n" +
                        "        AND ([t4].[CreateDate] >= [t0].[ReceivedDate])  \n" +
                        "        AND ([t3].[ProductID] = [t0].[ProductID]) \n" +
                        "        ),0)) AS [value] \n" +
                        "    FROM [dbo].[Product] AS [t0] \n" +
                        "    ) AS [t5] \n" +
                        "WHERE [t5].[value] >= 0";
            DataTable dtDuLieu = new Lists.DataAccess().ExecuteSelect(query, new SqlParameter("@ThoiDiem",dtAtTime));
            DataRow[] dr = dtDuLieu.Select("InStockQuantity>0");
            if (dr.Length==0)
            {
                return;
            }
            decimal total_10 = 0;
            decimal total_10_20 = 0;
            decimal total_20_50 = 0;
            decimal total_50_70 = 0;
            decimal total_70_100 = 0;
            decimal total_100 = 0;
            foreach (DataRow row in dtDuLieu.Rows)
            {
                decimal ss = (decimal) row["InStockQuantity"];
                if (ss<=10)
                {
                    total_10 += ss;
                }
                else if(ss>10 && ss<=20)
                {
                    total_10_20 += ss;
                }
                else if (ss > 20 && ss <= 50)
                {
                    total_20_50 += ss;
                }
                else if (ss > 50 && ss <= 70)
                {
                    total_50_70 += ss;
                }
                else if (ss > 70 && ss <= 100)
                {
                    total_70_100 += ss;
                }
                else if (ss > 100)
                {
                    total_100 += ss;
                }
            }

            dt.Rows.Add(1, "< 10 Liter", total_10);
            dt.Rows.Add(2, "11 to 20 Liter", total_10_20);
            dt.Rows.Add(3, "21 to 50 Liter", total_20_50);
            dt.Rows.Add(4, "51 to 70 Liter", total_50_70);
            dt.Rows.Add(5, "71 to 100 Liter", total_70_100);
            dt.Rows.Add(6, "> 100 Liter", total_100);

            Series series = new Series("Series1", ViewType.Pie3D);

            series.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;
            series.PointOptions.ValueNumericOptions.Precision = 0;
            series.PointOptions.PointView = PointView.ArgumentAndValues;
           
            // Specify data members to bind the series.
            series.ArgumentScaleType = ScaleType.Qualitative;
            series.ArgumentDataMember = "Name";
            series.ValueScaleType = ScaleType.Numerical;
            series.ValueDataMembers.AddRange(new string[] { "Value" });

            ChartControl.Series.Add(series);
            ChartControl.DataSource = dt;
        }
    }
}
